package org.nicocube.airain.re.utils;

import java.util.Date;
import java.util.Random;

import org.nicocube.airain.domain.client.character.Difficulty;
import org.nicocube.airain.domain.client.character.Expertise;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Alea {
	Logger log = LoggerFactory.getLogger(Alea.class);
	private static final int NB_DICE_CHALLENGE = 4; 

	private Random random;

	public Alea() {
		this.random = new Random(new Date().getTime());
	}
	
	public int challenge(Difficulty difficulty, Expertise expertise) {
		int result=0;
		for (int i=0; i < NB_DICE_CHALLENGE ; i++) result+=dice();
		result += (expertise.getModifier() - difficulty.getModifier());
		if (log.isDebugEnabled()) log.debug("challenge("+difficulty+","+expertise+")="+result);
		return result;
	}

	public boolean alea(double d) {
		return random.nextDouble() < d;
	}

	private int dice() {
		return random.nextInt(3)-1;
	}

}
